import Vue from 'vue'
import App from './App'
import { installRequest } from './utils/request'
import VueI18n from 'vue-i18n'
import Vuex from 'vuex'

// 导入 uni-ui 组件
import uniPopup from 'uni_modules/uni-popup/components/uni-popup/uni-popup.vue'
import uniTable from 'uni_modules/uni-table/components/uni-table/uni-table.vue'
import uniTh from 'uni_modules/uni-table/components/uni-th/uni-th.vue'
import uniTd from 'uni_modules/uni-table/components/uni-td/uni-td.vue'
import uniPagination from 'uni_modules/uni-pagination/components/uni-pagination/uni-pagination.vue'
import uniIcons from 'uni_modules/uni-icons/components/uni-icons/uni-icons.vue'
import uniTransition from 'uni_modules/uni-transition/components/uni-transition/uni-transition.vue'
import uniDatetimePicker from 'uni_modules/uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker.vue'
import uniBadge from 'uni_modules/uni-badge/components/uni-badge/uni-badge.vue'
import uniDateformat from 'uni_modules/uni-dateformat/components/uni-dateformat/uni-dateformat.vue'

// 导入其他需要的组件
import uniForms from 'uni_modules/uni-forms/components/uni-forms/uni-forms.vue'
import uniFormsItem from 'uni_modules/uni-forms/components/uni-forms-item/uni-forms-item.vue'
import uniList from 'uni_modules/uni-list/components/uni-list/uni-list.vue'
import uniListItem from 'uni_modules/uni-list/components/uni-list-item/uni-list-item.vue'
import uniFab from 'uni_modules/uni-fab/components/uni-fab/uni-fab.vue'
import uniSearchBar from 'uni_modules/uni-search-bar/components/uni-search-bar/uni-search-bar.vue'
import uniDataSelect from 'uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue'
import uniLoadMore from 'uni_modules/uni-load-more/components/uni-load-more/uni-load-more.vue'
import uniPopupDialog from 'uni_modules/uni-popup/components/uni-popup-dialog/uni-popup-dialog.vue'
import uniEasyinput from 'uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue'
import uniFilePicker from 'uni_modules/uni-file-picker/components/uni-file-picker/uni-file-picker.vue'
import uniDataCheckbox from 'uni_modules/uni-data-checkbox/components/uni-data-checkbox/uni-data-checkbox.vue'

// 注册组件
Vue.component('uni-popup', uniPopup)
Vue.component('uni-table', uniTable)
Vue.component('uni-th', uniTh)
Vue.component('uni-td', uniTd)
Vue.component('uni-pagination', uniPagination)
Vue.component('uni-icons', uniIcons)
Vue.component('uni-transition', uniTransition)
Vue.component('uni-datetime-picker', uniDatetimePicker)
Vue.component('uni-badge', uniBadge)
Vue.component('uni-dateformat', uniDateformat)

// 注册其他组件
Vue.component('uni-forms', uniForms)
Vue.component('uni-forms-item', uniFormsItem)
Vue.component('uni-list', uniList)
Vue.component('uni-list-item', uniListItem)
Vue.component('uni-fab', uniFab)
Vue.component('uni-search-bar', uniSearchBar)
Vue.component('uni-data-select', uniDataSelect)
Vue.component('uni-load-more', uniLoadMore)
Vue.component('uni-popup-dialog', uniPopupDialog)
Vue.component('uni-easyinput', uniEasyinput)
Vue.component('uni-file-picker', uniFilePicker)
Vue.component('uni-data-checkbox', uniDataCheckbox)

// 安装请求插件
installRequest(Vue)

// 全局错误处理
Vue.config.errorHandler = (err, vm, info) => {
  console.error('全局错误:', err)
  uni.showToast({
    title: '系统异常',
    icon: 'none'
  })
}

// 全局事件总线
Vue.prototype.$eventBus = new Vue()

// 引入权限配置
import permissions from './config/permissions'
Vue.prototype.$permissions = permissions

// 注册微信小店API
import wechatShopApi from './utils/wechatShopApi'
Vue.prototype.$wechatShopApi = wechatShopApi

Vue.use(VueI18n)

const i18n = new VueI18n({
  locale: 'zh-CN',
  fallbackLocale: 'zh-CN',
  silentTranslationWarn: true,
  messages: {
    'zh-CN': {
      common: {
        loading: '加载中...',
        error: '错误',
        success: '成功'
      },
      admin: {
        overview: '平台概览',
        partnerCount: '合作商数量',
        promoterCount: '推客总数',
        orderCount: '订单总数',
        salesAmount: '销售总额',
        review: '待审核',
        viewAll: '查看全部',
        noReview: '当前没有待审核的合作商',
        salesTrend: '销售趋势',
        partnerRank: '合作商排行',
        viewMore: '查看更多'
      },
      topwindow: {
        text: {
          changePwd: '修改密码',
          signOut: '退出登录'
        }
      }
    }
  }
})

Vue.config.productionTip = false

App.mpType = 'app'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    theme: 'light',
    locale: 'zh-CN'
  },
  mutations: {
    SET_THEME(state, theme) {
      state.theme = theme
    },
    SET_LOCALE(state, locale) {
      state.locale = locale
    }
  },
  actions: {
    setTheme({ commit }, theme) {
      commit('SET_THEME', theme)
    },
    setLocale({ commit }, locale) {
      commit('SET_LOCALE', locale)
    }
  }
})

const app = new Vue({
  i18n,
  store,
  ...App
})
app.$mount()